package com.ep.thirdPlatformProject.oa.controller;

import com.alibaba.excel.EasyExcel;
import com.ep.thirdPlatformProject.job.common.R;
import com.ep.thirdPlatformProject.oa.entity.dto.EcologyEfficiencyRankDTO;
import com.ep.thirdPlatformProject.oa.entity.vo.*;
import com.ep.thirdPlatformProject.oa.service.EcologyReportService;
import com.ep.thirdPlatformProject.system.aop.LoginRequired;
import com.ep.thirdPlatformProject.system.config.excel.CustomerHandler;
import com.ep.thirdPlatformProject.system.config.excel.ExcelCellWidthStyleStrategy;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Tag(name = "工时审核相关")
@RestController()
@RequestMapping("/ecology/api")
public class EcologyApiController {

    private final EcologyReportService reportService;


    /**
     * 按条件得到平均时间
     *
     * @param vo 签证官
     * @return {@link R }
     */
    @PostMapping("/getAvgTimeByCondition")
    @LoginRequired()
//    @ApiLimit(count = 1, timeLimit = 5, timeUnit = TimeUnit.HOURS)
    public R getAvgTimeByCondition(@Valid @RequestBody EfficiencyRankParamVO vo) {
        PageInfo<EcologyEfficiencyRankDTO> pageInfo = reportService.selectEfficiencyList(vo);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("list", pageInfo.getList());
        map.put("currentPage", vo.getCurrentPage());
        map.put("pageSize", vo.getPageSize());
        return R.success("请求成功", map);
    }


    /**
     * 获取待办事项工作流计数
     *
     * @param vo 签证官
     * @return {@link R }
     */
//    @ApiLimit(count = 1, timeLimit = 5, timeUnit = TimeUnit.HOURS)
    @PostMapping("/getTodoWorkflowCount")
    public R getTodoWorkflowCount(@Valid @RequestBody EfficiencyRankParamVO vo) {
        List<EcologyTodoWorkflowVO> vos = reportService.selectTodoWorkflowCount(vo);
        return R.success("请求成功", vos);
    }

    /**
     * 获取工作流超时时间计数
     *
     * @param vo 签证官
     * @return {@link R }
     */
//    @ApiLimit(count = 1, timeLimit = 5, timeUnit = TimeUnit.HOURS)
    @PostMapping("/getWorkflowOutTimeCount")
    public R getWorkflowOutTimeCount(@Valid @RequestBody EfficiencyRankParamVO vo) {
        List<EcologyTimeoutWorkflowVO> vos = reportService.selectWorkflowOutTimeCount(vo);
        return R.success("请求成功", vos);
    }

    /**
     * 完成工作流程
     *
     * @param vo 签证官
     * @return {@link R }
     */
//    @ApiLimit(count = 1, timeLimit = 5, timeUnit = TimeUnit.HOURS)
    @PostMapping("/getFinishedWorkflow")
    public R getFinishedWorkflow(@Valid @RequestBody EfficiencyRankParamVO vo) {
        List<EcologyFinishedWorkflowVO> vos = reportService.selectFinishedWorkflowCount(vo);
        return R.success("请求成功", vos);
    }

    /**
     * 获取工作流审核率
     *
     * @param vo 签证官
     * @return {@link R }
     */
//    @ApiLimit(count = 1, timeLimit = 5, timeUnit = TimeUnit.HOURS)
    @PostMapping("/getWorkflowAuditRate")
    public R getWorkflowAuditRate(@Valid @RequestBody EfficiencyRankParamVO vo) {
        List<EcologyWorkflowAuditRateVO> vos = reportService.selectWorkflowAuditRate(vo);
        return R.success("请求成功", vos);
    }

    @PostMapping("/outputAllExcelData")
    @Operation(summary  = "导出所有审核记录")
    public void outputAllExcelData(@Valid @RequestBody EfficiencyRankParamVO vo, HttpServletResponse response) {
        PageInfo<EcologyEfficiencyRankDTO> pageInfo = reportService.selectEfficiencyList(vo);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = "auditRecord";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            EasyExcel.write(response.getOutputStream(), EcologyEfficiencyRankDTO.class).sheet("审核工时明细表")
                    .registerWriteHandler(new CustomerHandler())
                    .registerWriteHandler(widthStyleStrategy)
                    .doWrite(pageInfo.getList());
        } catch (Exception e) {
            // 重置response
            response.reset();
            log.error("导出失败", e);
        }
    }

}
